To make the additions useful, we have to update the FileWorker class to read and insert the records into our Tutorials database.
Updating <FileWorker.ProcessFiles>
We need to create an instance of our LINQSQL
class and then use the file that we found while polling our process
folder to populate a new user record for each line in the process file.
To do this we will update the <ProcessFiles> method, which is where we query the outgoing folder, send an e-mail, and then move the file to our processed folder. Listing 1 shows the changes required for <ProcessFiles>.
Listing 1. FileWorker updated ProcessFiles method to support the LINQSQL class implementation.
Private Sub ProcessFiles() Dim LinqSql As LINQSQL = New LINQSQL() While Not m_ThreadAction.StopThread If Not m_ThreadAction.Pause Then Try For Each TextFile As String In My.Computer.FileSystem.GetFiles( _ m_FileWorkerOptions.Output, _ FileIO.SearchOption.SearchTopLevelOnly, _ m_FileWorkerOptions.FileType) If m_ThreadAction.Pause Or m_ThreadAction.StopThread Then Exit For End If Try Dim ProcessFile As String = _ m_FileWorkerOptions.ProcessedPath("\" + _ My.Computer.FileSystem.GetName(TextFile)) 'File is moved so lets read it out of the Output Folder If (Me.MailEnabled) Then Dim message As String Try message = "Processing File Data:[" + _ My.Computer.FileSystem.ReadAllText(TextFile) + _ "]] From File - " Catch ex As Exception message = "Unable to read from file - " End Try 'Send the Email and then move it again to the processed Folder m_FileWorkerOptions.EmailProperties.Message = message m_SmtpClient.QueueMail(m_FileWorkerOptions.EmailProperties) End If Dim records As StreamReader = _ My.Computer.FileSystem.OpenTextFileReader(TextFile) Dim record As String record = records.ReadLine() While (Not record Is Nothing) Try LinqSql.InsertRecord(record) Catch ex As Exception WriteLogEvent(My.Resources.ThreadIOError + _ "_" + ex.ToString + "_" + Now.ToString, THREAD_ERROR, _ EventLogEntryType.Error, My.Resources.Source) End Try record = records.ReadLine End While records.Close() records.Dispose() My.Computer.FileSystem.MoveFile(TextFile, ProcessFile, True) System.Threading.Thread.Sleep(0) WriteLogEvent(My.Resources.ThreadMessage + _ TextFile, THREAD_INFO, EventLogEntryType.Information, _ My.Resources.Source) Catch ex As Exception WriteLogEvent(My.Resources.ThreadErrorMessage + _ "_" + ex.ToString + "_" + Now.ToString, THREAD_ERROR, _ EventLogEntryType.Error, My.Resources.Source) End Try Next Catch fio As IOException WriteLogEvent(My.Resources.ThreadIOError + "_" + fio.ToString _ + "_" + Now.ToString, THREAD_ABORT_ERROR, _ EventLogEntryType.Error, My.Resources.Source) Catch tab As ThreadAbortException 'this must be listed first as Exception is the master catch 'Clean up thread here WriteLogEvent(My.Resources.ThreadAbortMessage + "_" _ + tab.ToString + "_" + Now.ToString, THREAD_ABORT_ERROR, _ EventLogEntryType.Error, My.Resources.Source) Catch ex As Exception WriteLogEvent(My.Resources.ThreadErrorMessage + "_" + _ ex.ToString + "_" + Now.ToString, THREAD_ERROR, _ EventLogEntryType.Error, My.Resources.Source) End Try End If If Not m_ThreadAction.StopThread Then Thread.Sleep(THREAD_WAIT) End If End While End Sub
|
The bolded code in Listing 7-6 creates an instance of our LINQSQL class before we enter our thread loop because we only need one instance of this class.
When a file is found, we open the file and iterate through it, passing each line to our <InsertRecord>
method on the LINQSQL instance. This will populate the database with a
new User instance. If the insertion fails, we log an event and move the
file.
Install and Verify
To validate that the
service can read files and create records for the system to insert, I
have modified my configuration.xml file so that it only points to the
incoming folder and looks for *.txt files.
I have created three files with five records each. Listing 2 is a sample from one of the text files.
Listing 2. Demonstration data used for service verification.
FirstName,LastName,111 South Street,NA,TestCity,TT,00000,555-1212 FirstName2,LastName,111 South Street,NA,TestCity,TT,00000,555-1212 FirstName3,LastName,111 South Street,NA,TestCity,TT,00000,555-1212 FirstName4,LastName,111 South Street,NA,TestCity,TT,00000,555-1212 FirstName5,LastName,111 South Street,NA,TestCity,TT,00000,555-1212
|
For each file I added five
records. Because there are no unique index constraints on any columns
but the UniqueIdentifier field, or GUID, the data shown in Listing 2 is appropriate.
You can run these
files through as many times as you like by merely cutting and pasting
them back into your incoming folder each time they are processed
successfully.
|